#include "CurrentContext.h"

class CurrentContext_Cleaner {
public:
    ~CurrentContext_Cleaner() {
        CurrentContext::destroy();
    }
} CurrentContext_cleaner_instance;

CurrentContext* CurrentContext::instance = NULL;

CurrentContext::CurrentContext() :
    m_activeLayerIndex(0)
{
    if(Debug::test(DEBUG_CREATE_DELETE)) {
        cout << "CurrentContext created" << endl;
    }
    m_available = 0;
}

CurrentContext::~CurrentContext()
{
}

CurrentContext* CurrentContext::get_instance() {
    if(!instance) {
        instance = new CurrentContext();
    }
    return instance;
}

void CurrentContext::destroy() {
    if(instance) {
        delete(instance);
        instance = NULL;
        if(Debug::test(DEBUG_CREATE_DELETE)) {
            cout << "CurrentContext destroyed" << endl;
        }
    }
}

uint CurrentContext::getCurrentLayerIndex()
{
    return m_activeLayerIndex;
}

void CurrentContext::setCurrentLayerIndex(uint index)
{
    m_activeLayerIndex = index;
}

bool CurrentContext::getActiveSpriteInfo(uint &activeSpriteInstanceIndex, uint &activeSpriteInstanceImageIndex,
    uint &activeSpriteInstanceSpriteIndex, SDL_Rect &activeSprtieRect) const
{
    if(!(m_available & ACTIVE_SPRITE)) {
        if (Debug::test(DEBUG_INFO)) {
            cout << "!(available & ACTIVE_SPRITE)" << endl;
        }
        return false;
    }
    activeSpriteInstanceIndex = m_activeSpriteInstanceIndex;
    activeSpriteInstanceImageIndex = m_activeSpriteInstanceImageIndex;
    activeSpriteInstanceSpriteIndex = m_activeSpriteInstanceSpriteIndex;
    activeSprtieRect = m_activeSprtieRect;
    if (Debug::test(DEBUG_INFO)) {
        cout << "GET Current active sprite : iid " << m_activeSpriteInstanceIndex
            << "LayerId" << m_activeLayerIndex
            << " imgId " << m_activeSpriteInstanceImageIndex
            << " spriteId " << m_activeSpriteInstanceSpriteIndex
            << " x " << m_activeSprtieRect.x << " y " << m_activeSprtieRect.y
            << " w " << m_activeSprtieRect.w << " h " << m_activeSprtieRect.h << endl;
    }
    return true;
}

bool CurrentContext::setActiveSpriteInfo(uint &activeSpriteInstanceIndex, uint &activeSpriteInstanceImageIndex,
    uint &activeSpriteInstanceSpriteIndex, SDL_Rect &activeSprtieRect)
{
    m_available |= ACTIVE_SPRITE;
    m_activeSpriteInstanceIndex = activeSpriteInstanceIndex;
    m_activeSpriteInstanceImageIndex = activeSpriteInstanceImageIndex;
    m_activeSpriteInstanceSpriteIndex = activeSpriteInstanceSpriteIndex;
    m_activeSprtieRect = activeSprtieRect;
    if (Debug::test(DEBUG_INFO)) {
        cout << "SET Current active sprite : iid" << m_activeSpriteInstanceIndex
            << "LayerId" << m_activeLayerIndex
            << " imgId "<< m_activeSpriteInstanceImageIndex
            << " spriteId " << m_activeSpriteInstanceSpriteIndex
            << " x " << m_activeSprtieRect.x << " y " << m_activeSprtieRect.y
            << " w " << m_activeSprtieRect.w << " h " << m_activeSprtieRect.h << endl;
    }
    return true;
}
